﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Web;
using CSharp.Libs.Net;
using CSharp.Libs.Koc;
using System.IO;

namespace KocGetSeed
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
            http://www70.kingdomsofcamelot.com/fb/e2/src/ajax/allianceGetOtherInfo.php?
             * cityId=10363&
             * fb_sig=1094f248ed089ad280d43e842be4ad86&
             * fb_sig_added=1&
             * fb_sig_api_key=0ab5e11ff842ddbdbf51ed7938650b3f&
             * fb_sig_app_id=130402594779&
             * fb_sig_base_domain=kingdomsofcamelot.com&
             * fb_sig_cookie_sig=be7dc09b89ae5066a03099baea30b5fc&
             * fb_sig_country=de&
             * fb_sig_expires=1299236400&
             * fb_sig_ext_perms=status_update%2Cphoto_upload%2Cvideo_upload%2Cemail%2Ccreate_note%2Cshare_item%2Cpublish_stream%2Cuser_birthday%2Cuser_religion_politics%2Cuser_relationships%2Cuser_relationship_details%2Cuser_hometown%2Cuser_location%2Cuser_likes%2Cuser_activities%2Cuser_interests%2Cuser_education_history%2Cuser_work_history%2Cuser_online_presence%2Cuser_website%2Cuser_groups%2Cuser_events%2Cuser_photos%2Cuser_videos%2Cuser_photo_video_tags%2Cuser_notes%2Cuser_about_me%2Cuser_status%2Cfriends_birthday%2Cfriends_religion_politics%2Cfriends_relationships%2Cfriends_relationship_details%2Cfriends_hometown%2Cfriends_location%2Cfriends_likes%2Cfriends_activities%2Cfriends_interests%2Cfriends_education_history%2Cfriends_work_history%2Cfriends_online_presence%2Cfriends_website%2Cfriends_groups%2Cfriends_events%2Cfriends_photos%2Cfriends_videos%2Cfriends_photo_video_tags%2Cfriends_notes%2Cfriends_about_me%2Cfriends_status&
             * fb_sig_in_iframe=1&
             * fb_sig_in_new_facebook=1&
             * fb_sig_locale=de_DE&
             * fb_sig_profile_update_time=1290143539&
             * fb_sig_session_key=2.8tl0wJ2l6D3XVUTXRH7sfw__.3600.1299236400-1353915001&
             * fb_sig_ss=3Yc0gMozgV_pZdFoke66Vg__&
             * fb_sig_time=1299232385.7612&
             * fb_sig_user=1353915001&
             * kabamuid=231889&
             * lang=en&
             * pageNo=1&
             * standalone=0&
             * tpuid=-YWu0zOci0yasStnHqrbBuQF84k&
             * tvuid=3176423
             */

            /*
            http://apps.facebook.com/kingdomsofcamelot/main_src.php?g=M&y=0&n=swaves&l=de_DE&messagebox=&standalone=0&res=1&iframe=1&fbml_sessionkey=2.bCFGWCJzRDCUMmKuW6LjQA__.3600.1299164400-100001079823973&lang=en&ts=1299158499.0836&page=feedback&s=70&appBar=swaves
             * g=M&
             * y=0&
             * n=swaves&
             * l=de_DE&
             * messagebox=&
             * standalone=0&
             * res=1&
             * iframe=1&
             * fbml_sessionkey=2.bCFGWCJzRDCUMmKuW6LjQA__.3600.1299164400-100001079823973&
             * lang=en&
             * ts=1299158499.0836&
             * page=feedback&
             * s=70&
             * appBar=swaves
             */
            //http://www70.kingdomsofcamelot.com/fb/e2/src/ajax/main_src.php?s=70&fb_sig=50c51ef9749682c83f883a25fd3aa5d5&fb_sig_added=1&fb_sig_api_key=0ab5e11ff842ddbdbf51ed7938650b3f&fb_sig_app_id=130402594779&fb_sig_base_domain=kingdomsofcamelot.com&fb_sig_cookie_sig=dbbd9e03265493c623b800b64d1d1b91&fb_sig_country=de&fb_sig_expires=1299690000&fb_sig_ext_perms=status_update%2Cphoto_upload%2Cvideo_upload%2Cemail%2Ccreate_note%2Cshare_item%2Cpublish_stream%2Cuser_birthday%2Cuser_religion_politics%2Cuser_relationships%2Cuser_relationship_details%2Cuser_hometown%2Cuser_location%2Cuser_likes%2Cuser_activities%2Cuser_interests%2Cuser_education_history%2Cuser_work_history%2Cuser_online_presence%2Cuser_website%2Cuser_groups%2Cuser_events%2Cuser_photos%2Cuser_videos%2Cuser_photo_video_tags%2Cuser_notes%2Cuser_about_me%2Cuser_status%2Cfriends_birthday%2Cfriends_religion_politics%2Cfriends_relationships%2Cfriends_relationship_details%2Cfriends_hometown%2Cfriends_location%2Cfriends_likes%2Cfriends_activities%2Cfriends_interests%2Cfriends_education_history%2Cfriends_work_history%2Cfriends_online_presence%2Cfriends_website%2Cfriends_groups%2Cfriends_events%2Cfriends_photos%2Cfriends_videos%2Cfriends_photo_video_tags%2Cfriends_notes%2Cfriends_about_me%2Cfriends_status&fb_sig_in_iframe=1&fb_sig_in_new_facebook=1&fb_sig_locale=de_DE&fb_sig_profile_update_time=1290143539&fb_sig_session_key=2.K5Wj2qldsehBEs7SYCJZ3A__.3600.1299690000-1353915001&fb_sig_ss=i49R7iQoZs7PdsHSBtURAw__&fb_sig_time=1299684350.5355&fb_sig_user=1353915001&lang=en&&standalone=0&tpuid=-YWu0zOci0yasStnHqrbBuQF84k&tvuid=3176423
            URLHolder UH = new URLHolder(new CSharp.Libs.ExceptionHandling.ExceptionCollector());
            //UH.ParseURLMainSrc("http://apps.facebook.com/kingdomsofcamelot/main_src.php?g=M&y=0&n=swaves&l=de_DE&messagebox=&standalone=0&res=1&iframe=1&fbml_sessionkey=2.bCFGWCJzRDCUMmKuW6LjQA__.3600.1299164400-100001079823973&lang=en&ts=1299158499.0836&page=feedback&s=70&appBar=swaves");
            
            UH.ParseURLMainSrc("http://www70.kingdomsofcamelot.com/fb/e2/src/main_src.php?g=M&y=0&n=organic&l=de_DE&messagebox=&standalone=0&res=1&iframe=1&fbml_sessionkey=2.lmSpOvEHVFhzbJqbJqdU9Q__.3600.1299693600-1353915001&lang=en&ts=1299686884.3509&s=70&appBar=");
            
            if(UH.URLMainSrc.URLMaybeValid)
            {
            //    UH.URLMainSrc.Parameters["fbml_sessionkey"] = "2.2iphrOclimd5HK4Px5sNWg__.3600.1299402000-1353915001";
            //    UH.URLMainSrc.Parameters["ts"] = "1299395256.3591";
            }

            String strURL = UH.URLMainSrc.GetURL();
            String strResult = "";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);

            // Set some reasonable limits on resources used by this request
            request.MaximumAutomaticRedirections = 4;
            request.MaximumResponseHeadersLength = 4;

            //Testing
            request.Method = "GET";
            //request.Credentials = new CredentialCache();

            // Set credentials to use for this request.
            //request.Credentials = CredentialCache.DefaultCredentials;
            
            HttpWebResponse response = (HttpWebResponse)request.GetResponse ();

            Console.WriteLine ("Content length is {0}", response.ContentLength);
            Console.WriteLine ("Content type is {0}", response.ContentType);

            // Get the stream associated with the response.
            Stream receiveStream = response.GetResponseStream ();

            // Pipes the stream to a higher level stream reader with the required encoding format. 
            StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);

            Console.WriteLine ("Response stream received.");
            strResult = readStream.ReadToEnd();


            
            
            
            String strSeek = "top.location.href";
            Int32 intStartIndex = strResult.IndexOf("\"", strResult.IndexOf(strSeek) + strSeek.Length);
            Int32 intEndIndex = strResult.IndexOf("\"", intStartIndex + 1);
            String strNextURL = strResult.Substring(intStartIndex + 1, intEndIndex - intStartIndex);

            
            
            
            
            
            Console.WriteLine (strResult);
            response.Close ();
            readStream.Close ();








            request = (HttpWebRequest)WebRequest.Create(strNextURL);

            // Set some reasonable limits on resources used by this request
            request.MaximumAutomaticRedirections = 4;
            request.MaximumResponseHeadersLength = 4;

            //Testing
            request.Method = "GET";
            //request.Credentials = new CredentialCache();

            // Set credentials to use for this request.
            //request.Credentials = CredentialCache.DefaultCredentials;

            response = (HttpWebResponse)request.GetResponse();

            Console.WriteLine("Content length is {0}", response.ContentLength);
            Console.WriteLine("Content type is {0}", response.ContentType);

            // Get the stream associated with the response.
            receiveStream = response.GetResponseStream();

            // Pipes the stream to a higher level stream reader with the required encoding format. 
            readStream = new StreamReader(receiveStream, Encoding.UTF8);

            Console.WriteLine("Response stream received.");
            strResult = readStream.ReadToEnd();            
            
            
            
            
            //BabyRay
            if (strResult != "")
            {
            }
        }
    }
}
